package com.ming.util;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 用户上下文工具类 - 用于获取当前登录用户信息
 */
public class UserContext {

    /**
     * 获取当前登录用户ID
     */
    public static Integer getCurrentUserId(HttpServletRequest request) {
        Object userId = request.getAttribute("userId");
        if (userId instanceof Integer) {
            return (Integer) userId;
        }
        return null;
    }

    /**
     * 获取当前登录用户名
     */
    public static String getCurrentUsername(HttpServletRequest request) {
        Object username = request.getAttribute("username");
        if (username instanceof String) {
            return (String) username;
        }
        return null;
    }

    /**
     * 获取当前登录用户姓名
     */
    public static String getCurrentName(HttpServletRequest request) {
        Object name = request.getAttribute("name");
        if (name instanceof String) {
            return (String) name;
        }
        return null;
    }

    /**
     * 检查用户是否已登录
     */
    public static boolean isLoggedIn(HttpServletRequest request) {
        return getCurrentUserId(request) != null;
    }
}